﻿var nbAnimaux = 1;
function Animal(x, y, type) {

    // Position des animaux
    this.X = x;
    this.Y = y;
    this.cote_sprite = null;
    var classAnimal = this;

    this.jeux = document.getElementById("jeux");
    this.animal = null;
    this.test = document.getElementById("test");
    this.type = type;

    this.direction = 3; //0 = Gauche, 1 = Haut, 2 = Droite, 3 = Bas
    this.orientation = "down";
    this.etat = -1; // -1: j'attends, 0: je vais tout droit 1: change une direction
    this.animal_img = {
        dino: {
            down: { 0: "images/animaux/dino2.png", 1: "images/animaux/dino1.png", 2: "images/animaux/dino2.png", 3: "images/animaux/dino3.png" },
            left: { 0: "images/animaux/dino5.png", 1: "images/animaux/dino4.png", 2: "images/animaux/dino5.png", 3: "images/animaux/dino6.png" },
            right: { 0: "images/animaux/dino8.png", 1: "images/animaux/dino7.png", 2: "images/animaux/dino8.png", 3: "images/animaux/dino9.png" },
            up: { 0: "images/animaux/dino11.png", 1: "images/animaux/dino10.png", 2: "images/animaux/dino11.png", 3: "images/animaux/dino12.png" },
            current: { src: "" }
        },
        poule: {
            down: { 0: "images/animaux/poule2.png", 1: "images/animaux/poule1.png", 2: "images/animaux/poule2.png", 3: "images/animaux/poule3.png" },
            left: { 0: "images/animaux/poule5.png", 1: "images/animaux/poule4.png", 2: "images/animaux/poule5.png", 3: "images/animaux/poule6.png" },
            right: { 0: "images/animaux/poule8.png", 1: "images/animaux/poule7.png", 2: "images/animaux/poule8.png", 3: "images/animaux/poule9.png" },
            up: { 0: "images/animaux/poule11.png", 1: "images/animaux/poule10.png", 2: "images/animaux/poule11.png", 3: "images/animaux/poule12.png" },
            current: { src: "" }
        },
        oiseau: {
            down: { 0: "images/animaux/oiseau2.png", 1: "images/animaux/oiseau1.png", 2: "images/animaux/oiseau2.png", 3: "images/animaux/oiseau3.png" },
            left: { 0: "images/animaux/oiseau5.png", 1: "images/animaux/oiseau4.png", 2: "images/animaux/oiseau5.png", 3: "images/animaux/oiseau6.png" },
            right: { 0: "images/animaux/oiseau8.png", 1: "images/animaux/oiseau7.png", 2: "images/animaux/oiseau8.png", 3: "images/animaux/oiseau9.png" },
            up: { 0: "images/animaux/oiseau11.png", 1: "images/animaux/oiseau10.png", 2: "images/animaux/oiseau11.png", 3: "images/animaux/oiseau12.png" },
            current: { src: "" }
        }
    };

    this.mouvement = 0;

    this.draw = function () {
        this.animal.style.left = this.X + "px";
        this.animal.style.top = this.Y + "px";
    }

    //Vérifie qu'une direction est disponible (déjà creusé) ou non.
    this.checkDirection = function (direc) {
        switch(direc){
            case 0: //Gauche
                if ((this.X - 50) / 50 >= 0 && classjeu.carte[(this.X - 50) / 50][(this.Y + 20) / 50].etat == true) {
                    return true;
                }
                break;
            case 1: //Haut
                if ((this.Y + 20 - 50) / 50 >= 0 && classjeu.carte[this.X / 50][(this.Y + 20 - 50) / 50].etat == true) {
                    return true;
                }
                break;
            case 2: //Droite
                if ((this.X+50)/50 < 16 && classjeu.carte[(this.X + 50) / 50][(this.Y + 20) / 50].etat == true) {
                    return true;
                }
                break;
            case 3: //Bas
                if ((this.Y + 20 + 50) / 50 < 16 && classjeu.carte[this.X / 50][(this.Y + 20 + 50) / 50].etat == true) {
                    return true;
                }
                break;
        }
        return false;
    }

    this.move = function () {

        //Si l'animal est sencé allé tout droit
        if (this.etat == 0) {
            //Soit il rencontre un obstacle et doit changer de direction
            if ((this.direction == 3) && !this.checkDirection(3)) {
                this.etat = 1;
            }
            else if ((this.direction == 0) && !this.checkDirection(0)) {
                this.etat = 1;
            }
            else if ((this.direction == 1) && !this.checkDirection(1)) {
                this.etat = 1;
            }
            else if ((this.direction == 2) && !this.checkDirection(2)) {
                this.etat = 1;
            }
            else if (this.checkDirection(this.direction)){ //Soit on continue de bouger dans la direction choisie
                switch (this.direction) {
                    case 0: //Gauche
                        this.X = this.animal.offsetLeft - 50;
                        this.cote_sprite.src = this.animal_img[this.type].left[this.mouvement % 4];
                        break;
                    case 1: //Haut
                        this.Y = this.animal.offsetTop - 50;
                        this.cote_sprite.src = this.animal_img[this.type].up[this.mouvement % 4];
                        break;
                    case 2: //Droite
                        this.X = 50 + this.animal.offsetLeft;
                        this.cote_sprite.src = this.animal_img[this.type].right[this.mouvement % 4];
                        break;
                    case 3: //Bas
                        this.Y = 50 + this.animal.offsetTop;
                        this.cote_sprite.src = this.animal_img[this.type].down[this.mouvement % 4];
                        break;

                }
            }
        }
        
        if (this.etat == -1 || this.etat == 1) { //Si on a décidé de changer de direction ou qu'on est dans l'état d'attente

                //Pour checker les côtés dans un ordre aléatoire
                var tab_alea = [1, 2, 3, 4];
                tab_alea.sort(function() { return 0.5 - Math.random() });
                for (var i = 0; i < 4; i++) {
                    switch(tab_alea[i]){

                        //+4 pour toujours avoir une valeur positive avec le modulo
                        case 1:
                            //On check à sa gauche 
                            if (this.checkDirection((this.direction + 4 - 1) % 4)) {
                                this.direction = (this.direction + 4 - 1) % 4;
                                this.etat = 0;
                            }
                       break;
                        case 2 :
                            //On check à sa droite
                            if (this.checkDirection((this.direction + 4 + 1) % 4)) {
                                this.direction = (this.direction + 4 + 1) % 4;
                                this.etat = 0;
                            }
                        break;
                        case 3:
                            //On check derrière
                            if (this.checkDirection((this.direction + 4 + 2) % 4)) {
                                this.direction = (this.direction + 4 + 2) % 4;
                                this.etat = 0;
                            }
                        break;
                        case 4 : 
                            //On check tout droit (au cas où..)
                            if (this.checkDirection(this.direction)) {
                                this.etat = 0;
                            }
                        break;
                    }
                }
                //Et on bouge si on a trouve une direction (etat = 0)
                if (this.etat == 0) {
                    switch (this.direction) {
                        case 0: //Gauche
                            this.X = this.animal.offsetLeft - 50;
                            this.cote_sprite.src = this.animal_img[this.type].left[this.mouvement % 4];
                            break;
                        case 1: //Haut
                            this.Y = this.animal.offsetTop - 50;
                            this.cote_sprite.src = this.animal_img[this.type].up[this.mouvement % 4];
                            break;
                        case 2: //Droite
                            this.X = 50 + this.animal.offsetLeft;
                            this.cote_sprite.src = this.animal_img[this.type].right[this.mouvement % 4];
                            break;
                        case 3: //Bas
                            this.Y = 50 + this.animal.offsetTop;
                            this.cote_sprite.src = this.animal_img[this.type].down[this.mouvement % 4];
                            break;
                    }
                }
            }

        this.mouvement++;

        //Empeche l'animal de dépasser les bords
        if (this.Y < -20) { this.Y = -20; }
        else if (this.Y > this.jeux.offsetHeight - this.animal.offsetHeight) { this.Y = this.jeux.offsetHeight - this.animal.offsetHeight; }
        if (this.X < 0) { this.X = 0; }
        else if (this.X > this.jeux.offsetWidth - 50) { this.X = this.jeux.offsetWidth - 50; }

        this.draw();
        if (perso.X == this.X && perso.Y == this.Y) {
            classjeu.gameover();
        }
    }

    this.init = function (x, y) {
        var img = document.createElement("img");
        img.alt = "";
        img.id = this.type + nbAnimaux;
        img.setAttribute("class", "animaux");
        img.setAttribute("data-win-bind", "src:src");
        this.jeux.appendChild(img);
        this.animal = img;
        nbAnimaux++;
        this.X = x;
        this.Y = y;
        WinJS.Binding.processAll(this.animal, this.animal_img[this.type].current);
        this.cote_sprite = WinJS.Binding.as(this.animal_img[this.type].current);
        this.cote_sprite.src = this.animal_img[this.type].down[0];

        this.draw();
    }

    this.init(this.X, this.Y);
}